package com.egghiro.framework.j2se.game.core.model.actor;

import java.awt.Point;

public abstract class Actor2D extends Layer implements IActor2D {

    /**
     * referencePixel(定位點)相對左上角的偏移
     * 默认位置为对象的中心。
     * 可以根據這個點所跟隨的目標的座標來設定當前对象的位置。
     */
    private Point referencePixel;

    public Actor2D(int width, int height) {
        super(width, height);

        referencePixel = new Point(getWidth() / 2, getHeight() / 2);
    }

    /**
     * 設定ReferencePixel相對Sprite左上角的偏移
     */
    public void defineReferencePixel(int rx, int ry) {
        referencePixel.move(rx, ry);
    }

    /**
     * Get the referencePixel.x relative to the upper-left corner of this Sprite
     * 
     * @return
     */
    public int getRefX() {
        return referencePixel.x;
    }

    /**
     * Get the referencePixel.y relative to the upper-left corner of this Sprite
     * 
     * @return
     */
    public int getRefY() {
        return referencePixel.y;
    }

    /**
     * Gets the horizontal position of this Sprite's reference pixel in the
     * painter's coordinate system.
     * 
     * @return
     */
    public int getRefPixelX() {
        return referencePixel.x + getX();
    }

    /**
     * Gets the vertical position of this Sprite's reference pixel in the
     * painter's coordinate system.
     * 
     * @return
     */
    public int getRefPixelY() {
        return referencePixel.y + getY();
    }

    /**
     * 
     * 根據referencePixel所跟隨的目標的座標來設定當前Sprite的位置
     * 
     * @param x
     *            referencePixel所跟隨的目標的x座標
     * @param y
     *            referencePixel所跟隨的目標的y座標
     */
    public void setRefPixelPosition(int x, int y) {
        // TODO
        setPosition(x - referencePixel.x, y - referencePixel.y);
    }

    /**
     * 碰撞點默認放在中心
     */
    public int getCollidePointX() {
        // TODO CollideRectangle
        return get2DX();
    }

    /**
     * 碰撞點默認放在中心
     */
    public int getCollidePointY() {
        // TODO CollideRectangle
        return get2DY();
    }

    // public abstract boolean isCollided(Actor2D other);

    // 圓形碰撞檢測可以基於距離
    // if(Math.pow(x1-x2,2) + Math.pow(y1-y2,2) < Math.pow(r1+r2,2)){
    // isCollided = true;
    // }
    //
    //
    // 複雜圖形可以使用組合碰撞檢測。也就是把一個單位分解成多個簡單圖形再檢測

    // 簡單的四邊形碰撞檢測邏輯
    // Function RectsOverlap:Int(x0:Float, y0:Float, w0:Float, h0:Float,
    // x2:Float, y2:Float, w2:Float, h2:Float)
    // If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
    // If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
    // Return True
    // End Function
}
